package com.example.shiroplusdemo.controller;

import com.example.shiroplusdemo.vcode.Captcha;
import com.example.shiroplusdemo.vcode.GifCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-07
 **/
@Controller
@Slf4j
public class LoginController {


    @RequestMapping("/login")
    public String login(){
//        requestScheme:http,requestServerName:localhost,requestServerPort:8080,requestContextPath:,requestServletPath:/login
//        requestContextPath项目名，根目录下为""，在前端中src引用是用找路径时用
        return "login";
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }


    @RequestMapping(value = "/ajaxLogin",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> submitLogin(@RequestParam("nickname")String nickname,@RequestParam("password")String password,
                                          @RequestParam("rememberMe")Boolean rememberMe,@RequestParam("vcode")String vcode){
        Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
        if(vcode == null||vcode==""){
            resultMap.put("status",500);
            resultMap.put("message","验证码不能为空!");
        }
        Session session = SecurityUtils.getSubject().getSession();
        vcode = vcode.toLowerCase();
        String v = (String)session.getAttribute("_code");
        if(!vcode.equals(v)){
            resultMap.put("status",500);
            resultMap.put("message","验证码错误");
            return resultMap;
        }

        try{
            UsernamePasswordToken token = new UsernamePasswordToken(nickname,password,rememberMe);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            resultMap.put("status",200);
            resultMap.put("message","登陆成功");
        }catch(Exception e){
            resultMap.put("status",500);
            resultMap.put("message",e.getMessage());
        }
        return resultMap;
    }

    @RequestMapping("/403")
    public String noPermission(){
        return "403";
    }

    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    @RequestMapping("/logout")
    public String logout(){
        try{
        SecurityUtils.getSubject().logout();
        } catch(Exception e){
            log.error(e.getMessage());
        }
        return "login";
    }

    @RequestMapping("/getGifCode")
    public void getGifCode(HttpServletResponse response,HttpServletRequest request){
        try{
            response.setHeader("Pragma","No-cache");
            response.setHeader("Cache-Control","no-cache");
            response.setDateHeader("Expires",0);
            response.setContentType("image/gif");
            /*
             * gif格式动画验证码
             * 宽，高，位数
             */
            Captcha captcha = new GifCaptcha(146,33,4);
            captcha.out(response.getOutputStream());
            HttpSession session = request.getSession(true);
            //存入Session 在登录与传进来的vcode进行比较
            session.setAttribute("_code",captcha.text().toLowerCase());
            log.info("获取验证码",captcha.text().toLowerCase());
        }catch (Exception e) {
            log.error("获取验证码异常",e.getMessage());
        }
    }
}